package com.antoco.controlcenter.view;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.antoco.controlcenter.base.BaseApplication;
import com.antoco.controlcenter.R;

import org.videolan.libvlc.util.VLCVideoLayout;


public class FloatView {

    private VLCVideoLayout playerView;
    private VlcManager vlcManager; //视频控制器
    private String url;
    private View convertView;
    private WindowManager wm;

    public void iniyPayer(View convertView) {
        // 视频播放
        playerView = convertView.findViewById(R.id.layout_player);
        if (vlcManager == null) {
            vlcManager = new VlcManager(BaseApplication.getContext(), playerView);

        }
    }

    public void play(String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        this.url = url;
        removeWindow();
        createFloatView();
        vlcManager.play(url);
    }

    public boolean isPlaying() {
        return vlcManager.isPlaying();
    }

    public void onResume() {
        if (null == vlcManager) {
            return;
        }
        vlcManager.play(url);
    }

    public void onStop() {
        if (null == vlcManager) {
            return;
        }
        vlcManager.stop();
    }

    public void onDestroy() {
        if (null == vlcManager) {
            return;
        }

        vlcManager.destroy();
        vlcManager = null;
    }

    public void createFloatView() {
//        if (TextUtils.isEmpty(url)) {
//            ToastUtils.showToast(BaseApplication.getContext(), "播放地址为空，无法创建悬浮窗");
//            return;
//        }
        convertView = LayoutInflater.from(BaseApplication.getContext())
                .inflate(R.layout.play_view, null);
        iniyPayer(convertView);


        wm = (WindowManager) BaseApplication.getContext().getSystemService(
                Context.WINDOW_SERVICE);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();

        // 设置window type
        if (Build.VERSION.SDK_INT >= 23) {
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
        /*
         * 如果设置为params.type = WindowManager.LayoutParams.TYPE_PHONE; 那么优先级会降低一些,
         * 即拉下通知栏不可见
         */

        params.format = PixelFormat.RGBA_8888; // 设置图片格式，效果为背景透明

        // 设置Window flag
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        /*
         * 下面的flags属性的效果形同“锁定”。 悬浮窗不可触摸，不接受任何事件,同时不影响后面的事件响应。
         * wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL |
         * LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE;
         */
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;

        // 设置悬浮窗的长得宽
        params.width = width / 3;
        params.height = height / 2;
        params.x = width - width / 2 - ((width / 3) / 2);
        params.y = height / 2 - 32;

        // 设置悬浮窗的Touch监听
        convertView.setOnTouchListener(new View.OnTouchListener() {
            int lastX, lastY;
            int paramX, paramY;

            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                        paramX = params.x;
                        paramY = params.y;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int dx = (int) event.getRawX() - lastX;
                        int dy = (int) event.getRawY() - lastY;
                        params.x = paramX + dx;
                        params.y = paramY + dy;
                        // 更新悬浮窗位置
                        wm.updateViewLayout(convertView, params);
                        break;
                }
                return true;
            }
        });
       // vlcManager.setFloatScreen(params.width,params.height);
        wm.addView(convertView, params);
    }

    //将小窗口从屏幕上移除
    public void removeWindow() {

        if (null != convertView && null != wm) {
            onDestroy();
            wm.removeView(convertView);
            convertView = null;
        }

    }

    //是否有悬浮窗口
    public boolean isWindowShowing() {
        return convertView != null && wm != null;
    }

}
